package com.unknow.first.dapp.admin.account.controller.admin;

import static com.unknow.first.dapp.admin.account.constants.FlashSwapConstants.FLASH_SWAP_MANAGER;
import static com.unknow.first.dapp.admin.account.constants.FlashSwapConstants.FLASH_SWAP_PAIR_MANAGER;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.unknow.first.dapp.admin.account.dto.FlashSwapPairCreateDTO;
import com.unknow.first.dapp.admin.account.dto.FlashSwapPairQueryDTO;
import com.unknow.first.dapp.admin.account.dto.FlashSwapPairUpdateDTO;
import com.unknow.first.dapp.admin.account.mapper.FlashSwapPair;
import com.unknow.first.dapp.admin.account.service.FlashSwapPairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant.AuthMethod;
import org.cloud.utils.MyBatisPlusUtil;
import org.cloud.vo.CommonApiResult;
import org.common.CommonPage;
import org.common.CommonParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flash/swap/pair/admin")
@Api(value = "管理员:兑换交易队API", tags = "管理员:兑换交易队API")
@SystemResource(path = "/flash/swap/pair/admin", parentMenuCode = FLASH_SWAP_MANAGER, parentMenuName = "闪兑管理")
public class AdminFlashSwapPairController {

    @Autowired
    FlashSwapPairService flashSwapPairService;

    @GetMapping
    @ApiOperation("查询闪兑交易队")
    @SystemResource(value = "/list", description = "查询闪兑交易队", authMethod = AuthMethod.BYUSERPERMISSION, menuCode = FLASH_SWAP_PAIR_MANAGER, menuName = "交易队管理")
    public CommonApiResult<CommonPage<FlashSwapPair>> getFLashSwapPair(FlashSwapPairQueryDTO queryDTO, CommonParam commonParam) {
        QueryWrapper<FlashSwapPair> queryWrapper = MyBatisPlusUtil.single().getPredicate(queryDTO);
        PageHelper.startPage(commonParam.getPage(), commonParam.getLimit(), commonParam.getSorts());
        return CommonApiResult.createSuccessResult(CommonPage.restPage(flashSwapPairService.list(queryWrapper)));
    }

    @PostMapping
    @ApiOperation("创建交易队")
    @SystemResource(value = "/createOrUpdate", description = "管理交易队", authMethod = AuthMethod.BYUSERPERMISSION)
    public CommonApiResult<Integer> create(@RequestBody FlashSwapPairCreateDTO pairCreateDTO) throws Exception {
        return CommonApiResult.createSuccessResult(flashSwapPairService.create(pairCreateDTO));
    }

    @PutMapping
    @ApiOperation("更新交易队")
    @SystemResource(value = "/createOrUpdate", description = "管理交易队", authMethod = AuthMethod.BYUSERPERMISSION)
    public CommonApiResult<Integer> update(@RequestBody FlashSwapPairUpdateDTO swapPairUpdateDTO) throws Exception {
        return CommonApiResult.createSuccessResult(flashSwapPairService.update(swapPairUpdateDTO));
    }

    @PutMapping("/status")
    @ApiOperation("更新交易队状态")
    @SystemResource(value = "/updateStatus", description = "更新交易队状态", authMethod = AuthMethod.BYUSERPERMISSION)
    public CommonApiResult<Integer> updateStatus(@RequestBody FlashSwapPairUpdateDTO swapPairUpdateDTO) throws Exception {

        FlashSwapPair flashSwapPairUpdate = FlashSwapPair.builder().status(swapPairUpdateDTO.getStatus()).swapPairId(swapPairUpdateDTO.getSwapPairId()).build();

        return CommonApiResult.createSuccessResult(flashSwapPairService.updateByPrimaryKeySelective(flashSwapPairUpdate));

    }

}
